#include "../../include/tasktype.h"
#include "../../include/task.h"

myTCB *FIFO_gettop(FIFOLIST *tskhandler)
{
	if (tskhandler->start == 0)
		return idleTask;
	else
		return tskhandler->start;
}

void FIFO_enqueue(FIFOLIST *tskhandler, myTCB *tsk)
{
	if (tskhandler->start == 0)
	{
		tskhandler->start = tskhandler->end = tsk;
		tskhandler->end->next = 0;
		return;
	}
	tskhandler->end->next = tsk;
	tskhandler->end = tsk;
	tsk->next=0;
	return;
}

myTCB* FIFO_dequeue(FIFOLIST *tskhandler)
{
	myTCB* TCB=tskhandler->start;
	if (tskhandler->start == 0)
		return TCB;
	if (tskhandler->start == tskhandler->end)
	{
		tskhandler->start = tskhandler->end = 0;
		return TCB;
	}
	tskhandler->start = tskhandler->start->next;
	return TCB;
}

myTCB *FIFO_destroy(FIFOLIST *tskhandler, int tid)
{
	myTCB *p = tskhandler->start;
	if (p == 0)
		return 0;
	if (p->tid == tid)
	{
		if (p == tskhandler->end)
			tskhandler->start = tskhandler->end = 0;
		else
		{
			tskhandler->start = p->next;
			p->next = 0;
		}
		return p;
	}
	while (p->next != 0)
	{
		if (p->next->tid == tid)
		{
			myTCB *q = p->next;
			p->next = q->next;
			q->next = 0;
			if (q == tskhandler->end)
				tskhandler->end = p;
			return q;
		}
		p = p->next;
	}
}
